﻿function SetOBJ_UnLoadBind() {
    $("[tag=dateyy]").unbind();    
    $("[tag=datevn]").unbind();
    $("[tag=datemmyy]").unbind();
    $("[tag=datetimevn]").unbind();
}

function SetOBJ_LoadBind() {
    $("[tag=datetimevn]").attr('maxlength', 16);
    $("[tag=datetimevn]").css('width', 100);
    $("[tag=datetimevn]").datetimepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
        
        /* fix buggy IE focus functionality */
        fixFocusIE: false,
        monthNamesShort: ["Thang 1", "Thang 2", "Thang 3", "Thang 4", "Thang 5", "Thang 6", "Thang 7", "Thang 8", "Thang 9", "Thang 10", "Thang 11", "Thang 12"],
        dayNamesMin: ["<font color=red>CN</font>", "T2", "T3", "T4", "T5", "T6", "T7"],
        maxDate: '+1y', minDate: new Date(2008, 1 - 1, 1),
        
        onSelect : function() { $(this).blur(); },
        onClose: function() { $(this).change(); },
        
        timeOnlyTitle: 'Chọn giờ',
        timeText: 'Thời gian',
        hourText: 'Giờ',
        minuteText: 'Phút',
        secondText: 'Giây',
        millisecText: 'Phần nghìn giây',
        timezoneText: 'Múi giờ',
        currentText: 'Hiện thời',
        closeText: 'Đóng',
        timeFormat: 'HH:mm',
        amNames: ['SA', 'AM', 'A'],
        pmNames: ['CH', 'PM', 'P'],
        isRTL: false
    });

    $("[tag=datetimevn]").mask('99/99/9999 99:99');
    
    
    $("[tag=datevn]").attr('maxlength', 10);
    $("[tag=datevn]").css('width', 70);
    $("[tag=datevn]").datepicker({
		dateFormat: 'dd/mm/yy',
		changeMonth: true,
		changeYear: true,

		/* fix buggy IE focus functionality */
		fixFocusIE: false,
		
		monthNamesShort: ["Thang 1","Thang 2","Thang 3","Thang 4","Thang 5","Thang 6","Thang 7","Thang 8","Thang 9","Thang 10","Thang 11","Thang 12"],
		dayNamesMin: ["<font color=red>CN</font>","T2","T3","T4","T5","T6","T7"],
		maxDate: '+1y', minDate: new Date(2008, 1 - 1, 1),
		
		onSelect : function() { $(this).blur(); },
        onClose  : function() { $(this).change(); }
	});

	$("[tag=datevn]").mask('99/99/9999');
    
	$("[tag=datemmyy]").attr('maxlength', 7);
	$("[tag=datemmyy]").css('width', 50);
	$("[tag=datemmyy]").focus(function() {
	    $(".ui-datepicker-calendar").hide();
	    $("#ui-datepicker-div").position({ my: "center top", at: "center bottom", of: $(this) });
	});
	$("[tag=datemmyy]").datepicker({
	    dateFormat: 'mm/yy',
	    changeMonth: true,
	    changeYear: true,

	    /* fix buggy IE focus functionality */
	    fixFocusIE: false,

	    monthNamesShort: ["Thang 1", "Thang 2", "Thang 3", "Thang 4", "Thang 5", "Thang 6", "Thang 7", "Thang 8", "Thang 9", "Thang 10", "Thang 11", "Thang 12"],
	    maxDate: '+1y', minDate: new Date(2008, 1 - 1, 1),
	    
	    onSelect: function() { $(this).blur(); },
	    onClose: function() { $(this).change(); },
	    onChangeMonthYear: function(year, month) { this.value = (month < 10 ? '0' : '') + month + '/' + year; }
	});

	$("[tag=datemmyy]").mask('99/9999');
    
	$("[tag=dateyy]").attr('maxlength', 4);
	$("[tag=dateyy]").css('width', 35);
	$("[tag=dateyy]").focus(function() {
	    $(".ui-datepicker-calendar").hide();
	    $(".ui-datepicker-prev, .ui-datepicker-next").hide();
	    $(".ui-datepicker-month").hide();
	    $("#ui-datepicker-div").position({ my: "center top", at: "center bottom", of: $(this) });
	});

	$("[tag=dateyy]").datepicker({
	    dateFormat: 'yy',
	    changeMonth: false,
	    changeYear: true,
	    stepMonths: 0,
	    showButtonPanel: false,
	    monthNamesShort: ["1","2","3","4","5","6","7","8","9","10","11","12"],
	    maxDate: '+3y', minDate: new Date(2012, 1 - 1, 1),
	    onChangeMonthYear: function(year) { this.value = year; }
	});

	$("[tag=dateyy]").mask('9999');
}

function pageLoad() { SetOBJ_UnLoadBind(); SetOBJ_LoadBind(); }




$(document).ready(function() {
    if ($("#divDuongPho") != null) {
        $("#divDuongPho").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 100,
            height: 'auto',
            width: 'auto',
            draggable: true,
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }

    if ($("#divKhachHang") != null) {
        $("#divKhachHang").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 100,
            height: 'auto',
            width: 'auto',
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }

    if ($("#divNhanVien") != null) {
        $("#divNhanVien").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 100,
            height: 'auto',
            width: 'auto',
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }

    if ($("#divDongHo") != null) {
        $("#divDongHo").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 100,
            height: 'auto',
            width: 'auto',
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }

    if ($("#divCQTT") != null) {
        $("#divCQTT").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 100,
            height: 'auto',
            width: 'auto',
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }

    if ($("#divHopDong") != null) {
        $("#divHopDong").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 100,
            height: 'auto',
            width: 'auto',
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }
    if ($("#divTTTT") != null) {
        $("#divTTTT").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 100,
            height: 'auto',
            width: 'auto',
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }
    if ($("#divDonDangKy") != null) {
        $("#divDonDangKy").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 20,
            height: 'auto',
            width: 'auto',
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }
    if ($("#divVatTu") != null) {
        $("#divVatTu").dialog({
            autoOpen: false,
            modal: true,
            minHeight: 20,
            height: 'auto',
            width: 'auto',
            resizable: false,
            open: function() {
                $(this).parent().appendTo($("form:first"));
            }
        });
    }
    if ($(".collapsibleContainer") != null)
        $(".collapsibleContainer").collapsiblePanel();

    $("#divLoading").dialog({
        autoOpen: false,
        modal: true,
        minHeight: 100,
        dialogClass: "loadingScreenWindow",
        closeOnEscape: false,
        height: 'auto',
        width: 'auto',
        resizable: false,
        draggable: true
    });
});

function openWaitingDialog() {
    var verPos = $(document).height() / 2;
    var horPos = $(document).width() / 2;

//    $("#divLoading").dialog("option", "title", "Xin chờ...");
    $("#divLoading").dialog("option", "position", [horPos - 120, verPos - 100]);
    $("#divLoading").dialog('open');
    $("#divLoading").block({
        css: { border: '0px' },
        fadeIn: 0,
        //fadeOut: 0,
        overlayCSS: { backgroundColor: '#ffffff', opacity: 1 }
    });
}

function closeWaitingDialog() {
    $.unblockUI();
    $("#divLoading").dialog('close');
}

function unblockWaitingDialog() {
    $("#divLoading").unblock();
}

function closeDialog(divId) {
    $.unblockUI();
    //Could cause an infinite loop because of "on close handling"
    $("#" + divId).dialog('close');
}

function openDialog(title, width, divId) {
    var horPos = $(document).width() / 2;

    $("#" + divId).dialog("option", "title", title);
    $("#" + divId).dialog("option", "position", [horPos - (width / 2), 100]);

    $("#" + divId).dialog('open');

    return;
}

function openDialogAndBlock(title, width, divId) {
    openDialog(title, width, divId);
    $("#" + divId).block({
//        message: '<h1>Xin chờ...</h1>',
        css: { border: '0px' },
        fadeIn: 0,
        overlayCSS: { backgroundColor: '#ffffff', opacity: 1 }
    });
}

function unblockDialog(divId) {
    $("#" + divId).unblock({
//        message: 'Xin chờ...',
        css: { border: '0px' },
        fadeIn: 0,
        overlayCSS: { backgroundColor: '#ffffff', opacity: 1 }
    });
}

function onClientClickGridItem(itemId) {
    openWaitingDialog();
    unblockWaitingDialog();

    __doPostBack(itemId, '');
}

function onClientClickGridDelete(itemId) {
    if (confirm('Xóa các record được chọn?')) {
        openWaitingDialog();
        unblockWaitingDialog();

        __doPostBack(itemId, '');
    }
}

function onUpdating() {
    openWaitingDialog();
    unblockWaitingDialog();
}

function onUpdated() {
    closeWaitingDialog();
}

function doPostBack(ctrl) {
    var ctrlId = ctrl.getAttribute("id").toString();
    var controlId = ctrlId.replace(/\_/g, '$').replace(/\:/g, '$');
    var controlNames = controlId.split('$');
    var controlName = controlNames[controlNames.length - 1];

    if (controlName == 'btnDelete') {
        if (confirm('Xóa các record được chọn?')) {
            openWaitingDialog();
            unblockWaitingDialog();
            __doPostBack(controlId, '');
        }

        return false;
    }

    openWaitingDialog();
    unblockWaitingDialog();

    __doPostBack(controlId, '');

    return false;
}

function doPostBackRefered(ctrlId, e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    jQuery.fn.exists = function() { return jQuery(this).length > 0; };

    if (code == 13) {
        String.prototype.endsWith = function(str) {
            return (this.match(str + '$') == str);
        };

        jQuery.expr[":"].asp = function(a, i, m) {
            return jQuery(a).attr('id') && jQuery(a).attr('id').endsWith(m[3]);
        };

        var ctrl = $(":asp('" + ctrlId + "')");
        var ctrlRealId = ctrl.attr("id").toString();
        var controlId = ctrlRealId.replace(/\_/g, '$').replace(/\:/g, '$');

        openWaitingDialog();
        unblockWaitingDialog();

        __doPostBack(controlId, '');

        return false;
    }
}